package com.kpx.app.view;

/**
 * 作者： Ryan
 * 描述：
 * 创建时间：2015/5/1 11:46.
 */

import android.content.Context;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.animation.Interpolator;

import java.lang.reflect.Field;

public class ViewPagerCustomDuration extends ViewPager {

    private final String TAG = getClass().getSimpleName();
    private GestureDetectorCompat mGestureDetectorCompat;
    private boolean mVerticalScroll = true;
    private ScrollDirectionListener mScrollDirectionListener;
    private ScrollerCustomDuration mScroller = null;
    private boolean mTouchable = true;

    public ViewPagerCustomDuration(Context context) {
        super(context);
        postInitViewPager();
    }

    public ViewPagerCustomDuration(Context context, AttributeSet attrs) {
        super(context, attrs);
        postInitViewPager();
    }

    /**
     * Override the Scroller instance with our own class so we can change the
     * duration
     */
    private void postInitViewPager() {
        try {
            Field scroller = ViewPager.class.getDeclaredField("mScroller");
            scroller.setAccessible(true);
            Field interpolator = ViewPager.class.getDeclaredField("sInterpolator");
            interpolator.setAccessible(true);
            mScroller = new ScrollerCustomDuration(getContext(), (Interpolator) interpolator.get(null));
            scroller.set(this, mScroller);
            setScrollDurationFactor(1.2);
            mGestureDetectorCompat = new GestureDetectorCompat(getContext(), new GestureDetector.SimpleOnGestureListener() {
                @Override
                public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                    float absDistanceX = Math.abs(distanceX);
                    float absDistanceY = Math.abs(distanceY);
                    if (absDistanceX == 0) {
                        absDistanceX = 0.1f;
                    }
                    if (absDistanceX > absDistanceY) {//角度小于27.5度并且 x 轴移动的距离大于 y 轴的距离时为横向滑动
                        if (mScrollDirectionListener != null) {
                            if (mScrollDirectionListener != null) {
                                if (distanceX > 0) {
                                    mScrollDirectionListener.onScroll(SCROLL_DIRECTION.SCROLL_LEFT, absDistanceX);
                                } else if (distanceX < 0) {
                                    mScrollDirectionListener.onScroll(SCROLL_DIRECTION.SCROLL_RIGHT, absDistanceX);
                                }
                            }
                            if (absDistanceY / absDistanceX < 0.5f) {
                                mVerticalScroll = false;
                            }
                        }
                    }
                    return super.onScroll(e1, e2, distanceX, distanceY);
                }
            }
            );
        } catch (Exception e) {
        }
    }

    /**
     * Set the factor by which the duration will change
     */
    public void setScrollDurationFactor(double scrollFactor) {
        mScroller.setScrollDurationFactor(scrollFactor);
    }

    /***
     * 设置滑动方向监听
     *
     * @param scrollDirectionListener
     */
    public void setScrollDirectionListener(ScrollDirectionListener scrollDirectionListener) {
        mScrollDirectionListener = scrollDirectionListener;
    }

    //图片缩放时java.lang.IllegalArgumentException: pointerIndex out of range解决方案

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        try {
            return super.onTouchEvent(ev);
        } catch (IllegalArgumentException ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public void setTouchable(boolean touchable) {
        mTouchable = touchable;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (!mTouchable) {
            return false;
        }
        try {
            mGestureDetectorCompat.onTouchEvent(ev);
            if (ev.getAction() == MotionEvent.ACTION_UP) {
                mVerticalScroll = true;
            }
            if (mVerticalScroll) {//如果是竖向滑动，则将触摸事件交给子View处理
                //                return false;
            }
            return super.onInterceptTouchEvent(ev);
        } catch (IllegalArgumentException ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public enum SCROLL_DIRECTION {
        SCROLL_LEFT,
        SCROLL_RIGHT
    }

    public interface ScrollDirectionListener {
        void onScroll(SCROLL_DIRECTION scrollDirection, float absDistanceX);
    }

}
